﻿using Forum.Filters;
using Forum.Models;
using Forum.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace Forum.Controllers
{
    public class ForumPostsController : Controller
    {
        ForumDb db = new ForumDb();
      
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }
        [Authorize]
        [HttpPost]
        [InitializeSimpleMembership]
        public ActionResult DoCreate(ForumPost forumPost)
        {
           
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var post = new ForumPost
                {
                    Content =Server.HtmlDecode(forumPost.Content),
                    CreationTime= DateTime.Now,
                    Title= forumPost.Title,
                    Author= user,
                };
            db.ForumPosts.Add(post);
            db.SaveChanges();
            return Redirect("/");
        }
        public ActionResult ViewPost(int postId)
        {
            if (!db.ForumPosts.Any(x=> x.Id==postId))
            {
                return Redirect("/");
            }
            var post = db.ForumPosts.Where(x => x.Id == postId)
               .Select(x => new FullForumPostViewModel
             {
                 Id = x.Id,
                 Content = x.Content,
                 CreationTime = x.CreationTime,
                 Title = x.Title,
                 Username = x.Author.UserName,
                 
             }).SingleOrDefault();

            post.Answers = db.ForumPostAnswers.Where(x => x.ForumPost.Id == postId)
            .Select(y => new ForumPostAnswersViewModel
                 {
                     Content = y.Content,
                     CreationTime = y.CreationTime,
                     EditTime = y.EditTime,
                     Username = y.Author.UserName,
                 });
            return View(post);
        }

        [Authorize]
        [HttpPost]
        [InitializeSimpleMembership]
        public ActionResult DoAnswer(int forumPostId, string content)
        {
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var forumPost = db.ForumPosts.FirstOrDefault(x => x.Id == forumPostId);


            db.ForumPostAnswers.Add(
                new ForumPostAnswer()
            {
            Author= user,
            Content = Server.HtmlDecode(content),
            CreationTime= DateTime.Now,
            EditTime = DateTime.Now,
            ForumPost= forumPost,
            });
            db.SaveChanges();
            return Redirect("/questions/" + forumPostId);
        }

        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult VoteUp(int id)
        {
            if (db.ForumPostVotes.Any(x => x.Post.Id == id && x.User.UserId == WebSecurity.CurrentUserId))
            {
                var currentEntry = db.ForumPostVotes.FirstOrDefault(x => x.Post.Id == id && x.User.UserId == WebSecurity.CurrentUserId);
                currentEntry.IsUpVote = true;
                db.SaveChanges();
            }
            else
            {
                var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
                var forumPost = db.ForumPosts.FirstOrDefault(x => x.Id == id);

                db.ForumPostVotes.Add(new ForumPostVote 
                {
                    Post= forumPost,
                    User= user,
                    IsUpVote= true,
                    
                });
                db.SaveChanges();
            }
            int rating = db.ForumPostVotes.Count(y => y.Post.Id == id && y.IsUpVote) -
                       db.ForumPostVotes.Count(y => y.Post.Id == id && !y.IsUpVote);
            return Content(rating.ToString());
        }

        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult VoteDown(int id)
        {
            if (db.ForumPostVotes.Any(x => x.Post.Id == id && x.User.UserId == WebSecurity.CurrentUserId))
            {
                var currentEntry = db.ForumPostVotes.FirstOrDefault(x => x.Post.Id == id && x.User.UserId == WebSecurity.CurrentUserId);
                currentEntry.IsUpVote = false;
                db.SaveChanges();
            }
            else
            {
                var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
                var forumPost = db.ForumPosts.FirstOrDefault(x => x.Id == id);

                db.ForumPostVotes.Add(new ForumPostVote
                {
                    Post = forumPost,
                    User = user,
                    IsUpVote = false,

                });
                db.SaveChanges();
            }
            int rating = db.ForumPostVotes.Count(y => y.Post.Id == id && y.IsUpVote) -
                       db.ForumPostVotes.Count(y => y.Post.Id == id && !y.IsUpVote);
            return Content(rating.ToString());
        }
    }
}
